package com.jmz.apt.api.Provider;

import com.jmz.apt.api.Utils.MD5Util;
import com.jmz.apt.api.entity.dbEntity.JmzAdminRole;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUser;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUserRoles;
import org.apache.ibatis.jdbc.SQL;

import java.sql.Date;

/**
 * Created by fanglexue on 2019/11/5.
 */
public class AdminUserRoleProvider {


    public String getTableName(){
        return "jmz_admin_user_roles";
    }

    public String saveAdminUserRole(JmzAdminUserRoles jmzAdminUserRoles){
        SQL sql = new SQL();
        sql.INSERT_INTO(getTableName());
        if (jmzAdminUserRoles.getRoleId() != null) {
            sql.VALUES("role_id", "#{roleId}");
        }

        if (jmzAdminUserRoles.getAdminId() != null) {
            sql.VALUES("admin_id", "#{adminId}");
        }

        //sql.VALUES("create_time", new Date());
        //sql.VALUES("add_time",);
        return sql.toString();
    }

    public String selectOne(JmzAdminUserRoles jmzAdminUserRoles) {
        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM(getTableName());
        if (jmzAdminUserRoles.getAdminId() != null) {
            sql.WHERE("admin_id=#{adminId}");
        }
        if (jmzAdminUserRoles.getId() != null) {
            sql.WHERE("id=#{id}");
        }
        if (jmzAdminUserRoles.getRoleId() != null) {
            sql.WHERE("role_id=#{roleId}");
        }
        String lastSql = sql.toString();
        return lastSql + " limit 1";
    }


    public String delete(JmzAdminUserRoles jmzAdminUserRoles) {
        return new SQL() {{
            DELETE_FROM(getTableName());
            if (jmzAdminUserRoles.getId() != null) {
                WHERE("id = #{id}");
            }
            if (jmzAdminUserRoles.getAdminId() != null) {
                WHERE("admin_id = #{adminId}");
            }
            if (jmzAdminUserRoles.getRoleId() != null) {
                WHERE("role_id = #{role_id}");
            }
        }}.toString();
    }

}
